/**
 * ResultPanel.java
 * @author pkendzo
 * Email: pkendzo@msn.com
 * 2012
 */
package lu.med.praxiswahl.wahlumed.client.cashbook;

import java.awt.Color;
import java.awt.Font;
import java.text.NumberFormat;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFormattedTextField;
import java.awt.SystemColor;
import javax.swing.SwingConstants;

/**
 * @author pkendzo
 * 
 */
public class ResultPanel extends JPanel implements CashBookModelListener
{

    /**
     * 
     */
    private static final long serialVersionUID = -8097465859207593022L;

    private Color black = new Color(0, 0, 190);

    private Font bold = new Font("Arial", Font.BOLD, 10);

    private JLabel periodLab;

    private JFormattedTextField inventoryBegin;
    private JLabel inventoryBeginLab;

    private JFormattedTextField totalRevenue;
    private JLabel totalRevenueLab;

    private JFormattedTextField totalSpending;
    private JLabel totalSpendingLab;

    private JFormattedTextField endInventory;
    private JLabel endInventoryLab;

    private JFormattedTextField verifyDateField;
    private JLabel verifyDateLabel;

    private JFormattedTextField signatureField;
    private JLabel signatureLab;
    private JFormattedTextField textField;
    private JFormattedTextField lblSollEndbestandField;
    
    private NumberFormat format;

    public ResultPanel()
    {

	super();
	setBackground(SystemColor.window);
	this.setLayout(null);
	this.setBounds(1050, 20, 802, 84);

	format = NumberFormat.getNumberInstance();
	format.setMinimumFractionDigits(2);
	
	this.init();
    }

    private void init()
    {
	
	this.periodLab = new JLabel("VON \t-\t BIS");
	this.periodLab.setForeground(black);
	this.periodLab.setFont(new Font("Verdana", Font.BOLD, 10));
	this.periodLab.setBounds(10, 11, 430, 20);

	this.inventoryBegin = new JFormattedTextField(format);
	inventoryBegin.setHorizontalAlignment(SwingConstants.CENTER);
	inventoryBegin.setFont(new Font("Verdana", Font.PLAIN, 11));
	inventoryBegin.setBackground(SystemColor.window);
	this.inventoryBegin.setEditable(false);
	this.inventoryBegin.setBounds(8, 51, 100, 20);
	this.inventoryBeginLab = new JLabel("Anfang Bestand");
	inventoryBeginLab.setBackground(SystemColor.window);
	inventoryBeginLab.setFont(new Font("Verdana", Font.PLAIN, 11));
	this.inventoryBeginLab.setBounds(8, 27, 102, 20);

	this.totalRevenue = new JFormattedTextField(format);
	totalRevenue.setHorizontalAlignment(SwingConstants.CENTER);
	totalRevenue.setFont(new Font("Verdana", Font.PLAIN, 11));
	totalRevenue.setBackground(SystemColor.window);
	this.totalRevenue.setEditable(false);
	this.totalRevenue.setBounds(118, 51, 100, 20);
	this.totalRevenueLab = new JLabel("(+) Einnahmen");
	totalRevenueLab.setBackground(SystemColor.window);
	totalRevenueLab.setFont(new Font("Verdana", Font.PLAIN, 11));
	this.totalRevenueLab.setBounds(118, 27, 102, 20);

	this.totalSpending = new JFormattedTextField(format);
	totalSpending.setHorizontalAlignment(SwingConstants.CENTER);
	totalSpending.setFont(new Font("Verdana", Font.PLAIN, 11));
	totalSpending.setBackground(SystemColor.window);
	this.totalSpending.setEnabled(false);
	this.totalSpending.setBounds(228, 51, 100, 20);
	this.totalSpendingLab = new JLabel("(-) Ausgaben");
	totalSpendingLab.setBackground(SystemColor.window);
	totalSpendingLab.setFont(new Font("Verdana", Font.PLAIN, 11));
	this.totalSpendingLab.setBounds(230, 27, 102, 20);

	this.endInventory = new JFormattedTextField(format);
	endInventory.setHorizontalAlignment(SwingConstants.CENTER);
	endInventory.setFont(new Font("Verdana", Font.PLAIN, 11));
	endInventory.setBackground(SystemColor.window);
	this.endInventory.setEditable(true);
	this.endInventory.setBounds(472, 51, 100, 20);
	this.endInventoryLab = new JLabel("Endbestand");
	endInventoryLab.setBackground(SystemColor.window);
	endInventoryLab.setFont(new Font("Verdana", Font.PLAIN, 11));
	this.endInventoryLab.setBounds(474, 27, 98, 20);

	this.verifyDateField = new JFormattedTextField(format);
	verifyDateField.setHorizontalAlignment(SwingConstants.CENTER);
	verifyDateField.setFont(new Font("Verdana", Font.PLAIN, 11));
	verifyDateField.setBackground(SystemColor.window);
	this.verifyDateField.setBounds(582, 51, 100, 20);
	this.verifyDateLabel = new JLabel("Geprueft am");
	verifyDateLabel.setForeground(Color.ORANGE);
	verifyDateLabel.setBackground(SystemColor.window);
	verifyDateLabel.setFont(new Font("Verdana", Font.PLAIN, 11));
	this.verifyDateLabel.setBounds(580, 27, 102, 20);

	this.add(this.inventoryBegin);
	this.add(this.inventoryBeginLab);

	this.add(this.totalRevenue);
	this.add(this.totalRevenueLab);

	this.add(this.totalSpending);
	this.add(this.totalSpendingLab);

	this.add(this.endInventory);
	this.add(this.endInventoryLab);

	this.add(this.periodLab);

	this.add(this.verifyDateField);
	this.add(this.verifyDateLabel);

	this.setBorder(BorderFactory.createTitledBorder("Zusammenfassung"));
	
	textField = new JFormattedTextField(format);
	textField.setHorizontalAlignment(SwingConstants.CENTER);
	textField.setFont(new Font("Verdana", Font.PLAIN, 11));
	textField.setBackground(SystemColor.window);
	textField.setBounds(692, 51, 100, 20);
	add(textField);
	textField.setColumns(10);
	
	JLabel lblGeprftDurch = new JLabel("Gepr\u00FCft durch");
	lblGeprftDurch.setForeground(Color.ORANGE);
	lblGeprftDurch.setBackground(SystemColor.window);
	lblGeprftDurch.setFont(new Font("Verdana", Font.PLAIN, 11));
	lblGeprftDurch.setBounds(692, 30, 98, 14);
	add(lblGeprftDurch);
	
	JLabel lblSollEndbestand = new JLabel("Soll - Endbestand");
	lblSollEndbestand.setFont(new Font("Verdana", Font.PLAIN, 11));
	lblSollEndbestand.setBackground(Color.WHITE);
	lblSollEndbestand.setBounds(344, 27, 120, 20);
	add(lblSollEndbestand);
	
	lblSollEndbestandField = new JFormattedTextField(format);
	lblSollEndbestandField.setHorizontalAlignment(SwingConstants.CENTER);
	lblSollEndbestandField.setFont(new Font("Verdana", Font.PLAIN, 11));
	lblSollEndbestandField.setEditable(false);
	lblSollEndbestandField.setBackground(Color.WHITE);
	lblSollEndbestandField.setBounds(342, 51, 120, 20);
	add(lblSollEndbestandField);
    }

    /**
     * @return the periodLab
     */
    public JLabel getPeriodLab()
    {
	return periodLab;
    }

    /**
     * @param periodLab
     *            the periodLab to set
     */
    public void setPeriodLab(JLabel periodLab)
    {
	this.periodLab = periodLab;
    }

    /**
     * @return the inventoryBegin
     */
    public JFormattedTextField getInventoryBegin()
    {
	return inventoryBegin;
    }

    /**
     * @param inventoryBegin
     *            the inventoryBegin to set
     */
    public void setInventoryBegin(JFormattedTextField inventoryBegin)
    {
	this.inventoryBegin = inventoryBegin;
    }

    /**
     * @return the totalRevenue
     */
    public JFormattedTextField getTotalRevenue()
    {
	return totalRevenue;
    }

    /**
     * @param totalRevenue
     *            the totalRevenue to set
     */
    public void setTotalRevenue(JFormattedTextField totalRevenue)
    {
	this.totalRevenue = totalRevenue;
    }

    /**
     * @return the totalSpending
     */
    public JFormattedTextField getTotalSpending()
    {
	return totalSpending;
    }

    /**
     * @param totalSpending
     *            the totalSpending to set
     */
    public void setTotalSpending(JFormattedTextField totalSpending)
    {
	this.totalSpending = totalSpending;
    }

    /**
     * @return the endInventory
     */
    public JFormattedTextField getEndInventory()
    {
	return endInventory;
    }

    /**
     * @param endInventory
     *            the endInventory to set
     */
    public void setEndInventory(JFormattedTextField endInventory)
    {
	this.endInventory = endInventory;
    }

    /**
     * @return the verifyDateField
     */
    public JFormattedTextField getVerifyDateField()
    {
	return verifyDateField;
    }

    /**
     * @param verifyDateField
     *            the verifyDateField to set
     */
    public void setVerifyDateField(JFormattedTextField verifyDateField)
    {
	this.verifyDateField = verifyDateField;
    }

    /**
     * @return the signatureField
     */
    public JFormattedTextField getSignatureField()
    {
	return signatureField;
    }

    /**
     * @param signatureField
     *            the signatureField to set
     */
    public void setSignatureField(JFormattedTextField signatureField)
    {
	this.signatureField = signatureField;
    }

    @Override
    public void modelChanged(CashBookDataModel model)
    {
	this.getPeriodLab().setText("VON \t-\t BIS");
	double totalRevenueByMonth = 0.0d;
	double totalSpendingByMonth = 0.0d;
	double totalBandDepositByMonth = 0.0d;
	double endInventoryByMonth = 0.0d;

	String period = "";

	ArrayList<CashBookDataRow> dataItems = model.getData();

	if (dataItems.size() > 0)
	{

	    period += dataItems.get(0).getDatum() + " - "
		    + dataItems.get(dataItems.size() - 1).getDatum();
	    this.getPeriodLab().setText(period);

	    for (int i = 0; i < dataItems.size(); i++)
	    {
		if (i == 0)
		{
		    this.getInventoryBegin().setText(
			    "" + dataItems.get(i).getInventory());
		}
		totalRevenueByMonth += dataItems.get(i).getRevenue();
		totalSpendingByMonth += Double.parseDouble(dataItems.get(i)
			.getSpending());
		totalBandDepositByMonth += dataItems.get(i).getBankDeposit();
	    }
//	    this.getTotalRevenue().setText("" + totalRevenueByMonth);
//	    this.getTotalSpending().setText("" + (totalSpendingByMonth + totalBandDepositByMonth));
	    
	    this.getTotalRevenue().setValue(totalRevenueByMonth);
	    this.getTotalSpending().setValue((totalSpendingByMonth + totalBandDepositByMonth));
	    

	    endInventoryByMonth = totalRevenueByMonth - (totalSpendingByMonth + totalBandDepositByMonth);
	    //this.getEndInventory().setText("" + endInventoryByMonth);
	    this.lblSollEndbestandField.setValue(endInventoryByMonth);

	    totalRevenueByMonth = 0.0d;
	    totalSpendingByMonth = 0.0d;
	}
	else{
	    this.clearPanel();
	}

    }

    /**
     * @return the lblSollEndbestandField
     */
    public JFormattedTextField getLblSollEndbestandField()
    {
        return lblSollEndbestandField;
    }

    /**
     * @param lblSollEndbestandField the lblSollEndbestandField to set
     */
    public void setLblSollEndbestandField(JFormattedTextField lblSollEndbestandField)
    {
        this.lblSollEndbestandField = lblSollEndbestandField;
    }

    public void clearPanel(){
	
	this.endInventory.setText("");
	this.getPeriodLab().setText("VON \t-\t BIS");
	this.inventoryBegin.setText("");
	this.totalRevenue.setText("");
	this.totalSpending.setText("");
	this.lblSollEndbestandField.setText("");
    }
}
